package readWirteExcel;

import model.ApiConfigDataModel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by yangxianming on 2015-06-19.
 */
public class CreateSqlConnection {
    ApiConfigDataModel acdm = null;

    public CreateSqlConnection(ApiConfigDataModel acdm){
        this.acdm = acdm;
    }
    public boolean createConnection() {
        String url = null;
        url = acdm.config.get("jdbcUrl");
        if (url == "" || url == null){
            System.out.println("jdbcUrl没有值,数据库连接没有创建");
            return false;
        }

        String user = acdm.config.get("username");
        String pw = acdm.config.get("password");

        try {
//            如果是sqlServer那就加载sqlServer驱动
            if (acdm.config.get("sqlName").equals("sqlServer")){
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            }
            Connection conn = DriverManager.getConnection(url, user, pw);
            acdm.statement = conn.createStatement();
        }catch (Exception se){
            System.out.println("数据库连接创建失败!请检查网络,excel的config配置,用户名密码等");
            se.printStackTrace();
            return false;
        }

        System.out.println("数据库连接创建成功");
        return true;
    }
}
